#!/bin/bash
##########  This is mysql script  ##########
##### Used to detect whether a database or table #####

##########  This is script name  ##########
fiilename=`basename $0`

#####  This is the mysql command path  #####
#mysql=/usr/local/mysql/bin/mysql     # This is a mysql command directory
mysql=/usr/bin/mysql     # This is a mysql command directory

##########    Show help    ###########
print_usage(){
    echo ""
    echo "Check whether there is the database:"
    echo "    Usage: $filename <-u ...> <-p ...> <-d ...>"
    echo "Check whether there is the table:"
    echo "    Usage: $filename <-u ...> <-p ...> <-d ...> <-t ...>"
    echo ""
    echo "Flags"
    echo " -u <username>: Please enter your mysql database username."
    echo " -p <password>: Please enter your mysql database user password."
    echo " -d <database>: Please input you want to test the database name."
    echo " -t <database>: Please input you want to test the table name."
    echo " -h : This parameter represents the help information"
}

########## get config data ############
if [ -z "$1" ]; then    # If $1 does not exist, it will display the following information.
    echo " Unknown argument: $1 "
    echo " Please input  -h for help. "
      exit $STATE_UNKNOWN
  else
    while [ "$#" -gt 0 ];do 
      case "$1" in
        -u)
          shift
          mysql_u=$1
          ;;
        -p)
          shift
          mysql_p=$1
          ;;
        -d)
          shift
          mysql_d=$1
          ;;
        -t)
          shift
          mysql_t=$1
          ;;
        -h)    
          print_usage
          exit $STATE_UNKNOWN
          ;;
        *)     # If the input is not the above parameters, the information will be displayed below the help.
          echo "Unknown argument:$*"
          echo "Please input -h for help information."
          exit $STATE_UNKNOWN
          ;;
      esac
    shift
    done
fi

##########  Nagios return codes  ##########
STATE_OK=0
STATE_WARNING=1
STATE_CRITICAL=2
STATE_UNKNOWN=3

##########   We want the data   ##########
database=`"$mysql" -u$mysql_u -p$mysql_p -e "show databases;" | grep -w "$mysql_d" | wc -l`
table=`"$mysql" -u$mysql_u -p$mysql_p -D "$mysql_d" -e "show tables;" | grep -w  "$mysql_t" | wc -l`

##########   print the result   ##########

if [ -n "$mysql_u" -a -n "$mysql_p" -a -n "$mysql_d" -a -z "$mysql_t" ]; then
    if [ "$database" -eq 0 ]; then
        echo "No $mysql_d this database in mysql."  
        exit 2 
      else 
        echo "$mysql_d this database in mysql."
        exit 0
    fi
  elif [ -n "$mysql_u" -a -n "$mysql_p" -a -n "$mysql_d" -a -n "$mysql_t" ]; then
    if [ "$table" -eq 0 ]; then
        echo "No $mysql_t this table in $mysql_d database."  
        exit 2
      else 
        echo "$mysql_t this table in $mysql_d database."
        exit 0
    fi
  else
    echo "Don't recognize the content of the input."
    echo "Usage: $filename <-u ...> <-p ...> <-d ...> <-t ...>"
    exit $STATE_UNKNOWN
fi


